/**    
 * 文件名：ImageComparator.java    
 *    
 * 版本信息：    
 * 日期：2014年3月10日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */
package opencvtest.chapter04;

import static com.googlecode.javacv.cpp.opencv_imgproc.CV_COMP_INTERSECT;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCompareHist;

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHistogram;

/**
 * Computes image similarity using `cvCompareHist`.
 */
public class ImageComparator {

    /**
     * Color reduction factor. The comparison will be made on images with the
     * color space reduced by this factor in each dimension
     */
    private final int colorReductionFactor = 32;

    private final ColorHistogram hist = new ColorHistogram();

    private final CvHistogram referenceHistogram;

    public IplImage referenceImage;

    public ImageComparator(IplImage referenceImage) {
        this.referenceHistogram = hist.getHistogram(referenceImage);
        this.referenceImage = referenceImage;
    }

    public double compare(IplImage image) {
        // ColorHistogram.colorReduce(image, colorReductionFactor);
        // show(image, "a");
        CvHistogram inputH = hist.getHistogram(image);
        return cvCompareHist(referenceHistogram, inputH, CV_COMP_INTERSECT);
    }
}
